﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using PPAP.BO;

namespace PPAP.Web.Master
{
    public partial class Users : PageBase
    {
        UsersBO bo = new UsersBO();
        DepartmentBO dbo = new DepartmentBO();
        LogBO lbo = new LogBO();
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                DataSet dlist = dbo.GetList("");
                foreach (DataRow dr in dlist.Tables[0].Rows)
                {
                    ddlDepartment.Items.Add(new ListItem(dr["DEPARTMENT"].ToString(), dr["ID"].ToString()));
                }
                DataSet plist = dbo.GetRoles();
                foreach (DataRow dr in plist.Tables[0].Rows)
                {
                    ddlPosition.Items.Add(new ListItem(dr["rolename"].ToString(), dr["rolename"].ToString()));
                }
                if (Request.QueryString["DID"] != null)
                {
                    LoadDep(Request.QueryString["DID"].ToString());
                }
                
                LoadData();
            }
            if (Request.QueryString["deid"] != null)
            {
                dele(Request.QueryString["deid"].ToString());
            }
        }



        private void LoadData()
        {
            DataSet ds = bo.GetList("");
            AspNetPager2.RecordCount = ds.Tables[0].Rows.Count;
            GridView1.DataSource = GetPageTable(ds.Tables[0], AspNetPager2.PageSize, AspNetPager2.CurrentPageIndex);
            GridView1.DataBind();
        }

        private void dele(string id)
        {
           int num= bo.Delete(int.Parse(id));
           lbo.WriteLog(CurrentUser.Id, "Delete User");
           if (num > 0)
               AlertMessage("Deleted successful");
           LoadData();
        }

        private void LoadDep(string id)
        {
            Model.Users d = bo.GetModel(int.Parse(id));
            txtEmail.Text = d.EMAIL;
            txtFamilyname.Text = d.FAMILYNAME;
            txtFirstname.Text = d.FIRSTNAME;
            txtMobilephone.Text = d.MOBILEPHONE;
            txtUserName.Text = d.USERNAME;
            ddlDepartment.SelectedValue = d.DEPARTMENTID.ToString().Trim();
            ddlPosition.SelectedValue = d.POSITION.ToString().Trim();
            rbtN.Checked=d.ISEXECUTEIONSPECTIONTASK==1?true:false;
            txtUserName.Enabled = false;
            Button1.Text = "Save";
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Model.Users d = new Model.Users();
            int num = 0;
            if (txtFirstname.Text.Trim() == "" || txtFamilyname.Text.Trim() == ""||txtUserName.Text.Trim()=="")
            {
                AlertMessage("The text frame not is empty");
                return;
            }
            if(Button1.Text=="Save")
                d = bo.GetModel(int.Parse(Request.QueryString["DID"].ToString()));
            d.DEPARTMENTID = int.Parse(ddlDepartment.SelectedValue);
            d.EMAIL = txtEmail.Text;
            d.FAMILYNAME = txtFamilyname.Text;
            d.FIRSTNAME = txtFirstname.Text;
            d.ISEXECUTEIONSPECTIONTASK = rbtY.Checked ? 0 : 1;
            d.MOBILEPHONE = txtMobilephone.Text;
            d.POSITION = ddlPosition.SelectedValue;
            d.USERNAME = txtUserName.Text;
            if (Button1.Text == "Add")
            {
                if (bo.GetList(" username='"+d.USERNAME+"'").Tables[0].Rows.Count > 0)
                {
                    AlertMessage("User name already exists");
                    return;
                }
                d.PASSWORD = "123456";
                num = bo.Add(d);
                lbo.WriteLog(CurrentUser.Id, "Add User:"+d.USERNAME);
            }
            else
            {
                d.ID = int.Parse(Request.QueryString["DID"].ToString());
                num = bo.Update(d);
                lbo.WriteLog(CurrentUser.Id, "Edit User:"+d.USERNAME);
            }
            if (num > 0)
            {
                txtUserName.Text = "";
                txtMobilephone.Text = "";
                txtFirstname.Text = "";
                txtFamilyname.Text = "";
                txtEmail.Text = "";
                txtUserName.Enabled = true;
                Button1.Text = "Add";
                //AlertMessage("Save success");
            }
            LoadData();
        }

        protected void AspNetPager2_PageChanged(object sender, EventArgs e)
        {
            LoadData();
        }

    }
}